package com.tju.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tju.JwtUtils;
import com.tju.Result;
import com.tju.eduservice.client.UcenterClient;
import com.tju.eduservice.entity.EduComment;
import com.tju.eduservice.entity.frontVo.UcenterMemberVo;
import com.tju.eduservice.service.EduCommentService;
import com.tju.eduservice.service.EduCourseService;
import com.tju.serviceHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author gsc
 * @since 2022-01-17
 */
@RestController
@RequestMapping("/eduservice/comment")
public class EduCommentController {

    @Autowired
    private EduCommentService commentService;

    @Autowired
    private UcenterClient ucenterClient;
    //根据课程id分页查询课程评论信息
    @GetMapping("/getCommentPage/{page}/{limit}/{courseId}")
    public Result getCommentPage(@PathVariable long page,
                                 @PathVariable long limit,@PathVariable(required = false) String courseId){
        Page<EduComment> commentPage = new Page<>(page, limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        System.out.println("courseId" + courseId);
        if (!StringUtils.isEmpty(courseId)){
            wrapper.eq("course_id",courseId);
        }
        //按照评论时间排序
        wrapper.orderByDesc("gmt_create");
        commentService.page(commentPage,wrapper);

        List<EduComment> commentList = commentPage.getRecords();
        long current = commentPage.getCurrent();
        long size = commentPage.getSize();
        long total = commentPage.getTotal();
        long pages = commentPage.getPages();
        boolean hasNext = commentPage.hasNext();
        boolean hasPrevious = commentPage.hasPrevious();

        HashMap<String, Object> map = new HashMap<>();
        map.put("current",current);
        map.put("size",size);
        map.put("total",total);
        map.put("pages",pages);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        map.put("list",commentList);

        return Result.ok().data(map);

    }

    //添加评论
    @PostMapping("/auth/addComment")
    public Result addComment(HttpServletRequest request,
                             @RequestBody EduComment eduComment){
        //根据token信息获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)){
            throw new GuliException(20001,"请登录后评论");
        }
        eduComment.setMemberId(memberId);
        //远程调用UCenter来获取用户信息
        UcenterMemberVo memberVo = ucenterClient.getMemberInfoById(memberId);
        eduComment.setAvatar(memberVo.getAvatar());
        eduComment.setNickname(memberVo.getNickname());

        //保存评论
        commentService.save(eduComment);
        return Result.ok();
    }
}

